package me.shurufa.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.Bind;
import me.shurufa.R;
import me.shurufa.fragments.DigestPreviewFragment;
import me.shurufa.model.AddDigestBean;
import me.shurufa.utils.Constants;
import me.shurufa.utils.Utils;
import me.shurufa.widget.simpledialog.SimpleDialog;

/**
 * 作者 : andy
 * 日期 : 16/3/14 14:19
 * 邮箱 : andyxialm@gmail.com
 * 描述 : 首页所有Menu Fragment内的图书点击都进入到图书的详情界面
 */
public class BookDigiestPreviewActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.left_text)
    TextView leftText;
    @Bind(R.id.right_image)
    ImageView rightImage;
    @Bind(R.id.right_text)
    TextView rightText;

    private String mImageFilePath;
    private DigestPreviewFragment mDigestPreviewFragment;
    private AddDigestBean mAddDigestBean;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_digest_preview;
        super.onCreate(savedInstanceState);
        parseArguments();
        initUI();
    }

    public static void initArguments(Intent intent, AddDigestBean addDigestBean) {
        intent.putExtra(Constants.ARG_ADD_DIGEST, addDigestBean);
    }

    /**
     * @deprecated 新版干掉了
     * @param intent
     * @param absolutePath
     * @param addDigestBean
     */
    public static void initArguments(Intent intent, String absolutePath, AddDigestBean addDigestBean) {
        intent.putExtra(Constants.EXTRA_CAMERA_PHOTO, absolutePath);
        intent.putExtra(Constants.ARG_ADD_DIGEST, addDigestBean);
    }
    /**
     * 解析参数
     */
    private void parseArguments() {
        mImageFilePath = getIntent().getData().getPath();
        mAddDigestBean = (AddDigestBean) getIntent().getSerializableExtra(Constants.ARG_ADD_DIGEST);
    }

    private void initUI() {
        titleTextView.setText(R.string.digest_preview);
        rightText.setText(getString(R.string.next));
        rightImage.setVisibility(View.GONE);
        rightText.setVisibility(View.VISIBLE);
        rightText.setOnClickListener(this);

        initFragment();
    }

    private void initFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        mDigestPreviewFragment = DigestPreviewFragment.newInstance(mImageFilePath, mAddDigestBean);
        ft.replace(R.id.fragment_container, mDigestPreviewFragment);
        ft.commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.right_text:
                onNext();
                break;
        }
    }

    /**
     * 右上角点击下一步
     */
    private void onNext() {

        // mDigestPreviewFragment.handle(); 保存图片到本地

        mAddDigestBean.content = mDigestPreviewFragment.getDigestContent();
        mAddDigestBean.screenShot = mDigestPreviewFragment.getDigestImageUrl();

        mAddDigestBean.type = mDigestPreviewFragment.getDigestType();

        // 若是文字书摘 则限制长度为10-500
        if (0 == mAddDigestBean.type) {
            if (!TextUtils.isEmpty(mAddDigestBean.content)) {
                if (mAddDigestBean.content.length() < 10) {
                    Utils.showToast("书摘内容不可少于10字");
                    return;
                }
                if (mAddDigestBean.content.length() > 500) {
                    Utils.showToast("书摘内容不可超过500字");
                    return;
                }
            } else {
                Utils.showToast("书摘内容不可少于10字");
                return;
            }
        } else {
            if (TextUtils.isEmpty(mAddDigestBean.screenShot)) {
                showRetryUploadDlg();
                return;
            }
        }

        if (null != mAddDigestBean && (!TextUtils.isEmpty(mAddDigestBean.content) || !TextUtils.isEmpty(mAddDigestBean.screenShot))) {
            Intent in = new Intent(this, BookDigiestAddNoteActivity.class);
            BookDigiestAddNoteActivity.initArguments(in, mImageFilePath, mAddDigestBean);
            startActivity(in);
        } else {
            Utils.showToast(getString(R.string.info_scan_digest));
        }
    }

    /**
     * 重新上传Dialog
     */
    private void showRetryUploadDlg() {
        SimpleDialog dialog = new SimpleDialog(this);
        dialog.setNegativeButtonVisibility(View.GONE);
        dialog.setContentText("图片上传失败, 请重试!");
        dialog.setOnPositiveListener("重新上传", new SimpleDialog.OnPositiveListener() {
            @Override
            public void onClick(SimpleDialog dialog) {
                dialog.dismiss();
                mDigestPreviewFragment.reqImageIdentify();
            }
        }).show();
    }

    @Override
    public void onEventMainThread(Intent intent) {
        super.onEventMainThread(intent);
        if (Constants.EVENT_COMPLETE_ADD_DIGEST.equals(intent.getAction())) {
            finish();
        }
    }
}
